//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.main.heath.response;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import javax.validation.ValidationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

public class Response<T> implements Serializable {
    private static final Logger logger = LoggerFactory.getLogger(Response.class);
    private Integer code;
    private String msg;
    private T data;

    public Response() {
        this.code = Response.CodeEnum.SUCCESS.code;
    }

    public Response(Integer code) {
        this.code = code;
    }

    public Response(String msg) {
        this.msg = msg;
    }

    public Response(Throwable throwable) {
        logger.error(throwable.getMessage(), throwable);
        if(!(throwable instanceof ValidationException) && !(throwable instanceof IllegalArgumentException)) {
            this.code = Response.CodeEnum.UNKNOWN.code;
            this.msg = throwable.getMessage();
        } else {
            this.code = Response.CodeEnum.PARAM_INVALID.code;
            this.msg = throwable.getMessage();
        }

    }

    public Response(Throwable throwable, Integer code) {
        logger.error(throwable.getMessage(), throwable);
        this.code = code;
        this.msg = throwable.getMessage();
    }

    public Response(T data) {
        this.code = Response.CodeEnum.SUCCESS.code;
        this.data = data;
    }

    public Response(T data, String msg) {
        this.code = Response.CodeEnum.SUCCESS.code;
        this.msg = msg;
        this.data = data;
    }

    public Response(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Response(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T businessResult) {
        this.data = businessResult;
    }

    public int getCode() {
        return this.code.intValue();
    }

    public void setCode(int code) {
        this.code = Integer.valueOf(code);
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public boolean isSuccess() {
        return this.code != null?Response.CodeEnum.SUCCESS.code == this.code:false;
    }

    public String toString() {
        return JSON.toJSONString(this, new SerializerFeature[]{SerializerFeature.WriteMapNullValue});
    }

    public static enum CodeEnum {
        SUCCESS(Integer.valueOf(0), "成功"),
        FAILED(Integer.valueOf(1), "失败"),
        PARAM(Integer.valueOf(10000000), "参数错误"),
        PARAM_MISS(Integer.valueOf(10010000), "参数缺失"),
        PARAM_INVALID(Integer.valueOf(10020000), "参数不合法"),
        PARAM_INVALID_FORMAT(Integer.valueOf(10020100), "参数格式错误"),
        PARAM_INVALID_OUT_OF_RANGE(Integer.valueOf(10020200), "参数越界"),
        AUTHEN(Integer.valueOf(11000000), "身份认证失败"),
        AUTHEN_SIGN(Integer.valueOf(11010000), "签名验证失败"),
        AUTHOR(Integer.valueOf(12000000), "权限认证失败"),
        UNKNOWN(Integer.valueOf(99999999), "未知错误");

        public Integer code;
        public String msg;

        private CodeEnum(Integer code, String msg) {
            this.code = code;
            this.msg = msg;
        }
    }
}
